/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean.content;

import com.jb.framework.PersistentContext;
import com.jb.web.bean.FloatPanel;
import com.jb.web.bean.Panel;
import com.jb.web.bean.Link;
import com.jb.web.context.WebContext;
import com.jb.web.renderer.RenderKitImpl;

import com.jb.faces.event.ActionEvent;
import com.jb.faces.event.ActionListener;


/**
 * A portal is a area in a browser where personalized information is displayed.
 *
 * This is the view part of the portlet.
 *
 * A content provider will be used to supply content, as well as edit the
 * configuration.
 */
public class Portal extends FloatPanel implements ActionListener {
    static final long serialVersionUID = 123123L;

    static {
        RenderKitImpl.registerRenderer(Portal.class, PortalRenderer.class);
        publish(Portal.class, GENERAL, "personalizable");
        publish(Portal.class, GENERAL, "showHeader");
    }

    Panel textBox;
    Link editLink;
    PortalContentProvider provider;

    public StringBuffer getContent(WebContext ctx) {
        return getProvider().getContent(ctx, this);
    }

    //
    //this is called when an instance is placed onto screen
    //
    public void setDefault(PersistentContext pCtx) {
        setCursor("default");
        setWidth("200px");
        setHeight("300px");
        setValue("Portal values");
        setStyleClass("portalBox");

        getTextBox().setValue("You can edit the value by click the edit button at runtime");
        getEditLink();
 
    }

    //create edit link on demand
    public Link getEditLink() {
        String linkId = getId() + "el";
        editLink = (Link) getFacet(linkId);

        if (editLink == null) {
            editLink = new Link();
            editLink.setId(getId() + "el");
            editLink.setDestination("javascript:void(0);");
            editLink.setOnClick(null);
            editLink.setValue("${resource.edit}");
            addFacet(editLink.getId(), editLink);
        }

        editLink.addActionListener(this);

        return editLink;
    }

    //create text box on demand
    public Panel getTextBox() {
        String boxId = getId() + "box";
        textBox = (Panel) getFacet(boxId);

        if (textBox == null) {
            textBox = new Panel();
            textBox.setId(getId() + "box");
            addFacet(textBox.getId(), textBox);
        }

        return textBox;
    }

    public void delete(PersistentContext ctx) {
        getProvider().delete((WebContext) ctx, this);
        super.delete(ctx);
    }

    //this is action event when the edit link is clicked
    public void processAction(ActionEvent ae) {
        WebContext ctx = (WebContext) getFacesContext();
        getProvider().edit(ctx, this);
    }

    private PortalContentProvider getProvider() {
        if (provider == null) {
            provider = new ContentEditor(isPersonalizable());
        }

        return provider;
    }
}
